<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/static/main.css">
    <title>Code-server</title>
</head>
<body>
    <iframe id="main" src="{{URL}}" sandbox="allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-presentation allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation allow-top-navigation-by-user-activation"></iframe>
    <script type="text/javascript">
        function getCookie(cookieName) {
            const strCookie = document.cookie
            const cookieList = strCookie.split(';')
            
            for(let i = 0; i < cookieList.length; i++) {
              const arr = cookieList[i].split('=')
              if (cookieName === arr[0].trim()) {
                return arr[1]
              }
            }
            
            return ''
          }
        if ("WebSocket" in window) {
            console.log('666')
            // 打开一个 web socket
            var ws = new WebSocket("ws://{{wsip}}");
            // 连接建立后的回调函数
            ws.onopen = function () {
                // Web Socket 已连接上，使用 send() 方法发送数据
                var x = document.cookie
                ws.send("keep!"+getCookie("username"));
                //console.log('已启动工作区!')
            };

            // 接收到服务器消息后的回调函数
            ws.onmessage = function (evt) {
                var received_msg = evt.data;
                if (received_msg.indexOf("sorry") == -1) {
                    var x = document.cookie
                    ws.send("keep!"+getCookie("username"));
                }
            };
            // 连接关闭后的回调函数
            ws.onclose = function () {
                // 关闭 websocket
                alert("连接已被关闭... 您的工作区会在一会儿被关闭!");
            };
        } else {
            // 浏览器不支持 WebSocket
            alert("您的浏览器不支持 WebSocket! 您的工作区会在一会儿被关闭!");
        }
    </script>
</body>
</html>